<template>
  <div class="search-ai pad">
    <div class="head">
      <span>查询AI模板</span>
    </div>
    <div class="search m-t-b">
      <el-input clearable placeholder="AI模板名称" v-model="searchTxt" @input="clearSearch" />
      <el-button type="primary" @click="handleSearch">
        <i class="el-icon-search"></i> 搜索
      </el-button>
    </div>
    <el-table class="m-t-b" border stripe :data="templates" v-loading="loading">
      <el-table-column label="AI模板名称" prop="aiTemplateName"></el-table-column>
      <el-table-column label="AI模板编号" prop="aiTemplateNo"></el-table-column>
      <el-table-column label="上传时间" prop="createDate"></el-table-column>
      <el-table-column label="操作" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button type="default" @click="lookAI(scope.row)" icon="el-icon-view" disabled circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="m-t-b flex"
      :page-size="pageSize"
      layout="prev, pager, next"
      @current-change="handlePageChange"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import { formatDate } from '../util'
import { aiTemplateList } from '../api'

export default {
  name: 'search-AI',
  data () {
    return {
      searchTxt: '',
      loading: true,
      total: 0,
      pageSize: 10,
      templates: []
    }
  },
  methods: {
    lookAI (data) {
      this.$router.push('/template/' + data.aiTemplateId)
    },
    clearSearch () {
      if (!this.searchTxt) {
        this.fetchTemplateList(1)
      }
    },
    handleSearch () {
      if (!this.$checkAuth('canSearchAITemplate')) {
        return this.$message.error('暂无对应操作权限!')
      };
      if (!this.searchTxt.trim()) {
        this.searchTxt = ''
        return this.$message.error('请输入搜索内容!')
      }
      this.fetchTemplateList(1, this.searchTxt)
    },
    handlePageChange (page) {
      if (this.searchTxt.trim()) {
        this.fetchTemplateList(page, this.searchTxt)
      } else {
        this.fetchTemplateList(page)
      }
    },
    fetchTemplateList (index, aiTemplateName) {
      this.loading = true
      aiTemplateName = aiTemplateName || null
      aiTemplateList({
        index,
        count: this.pageSize,
        queryParam: {
            aiTemplateName
        }
      })
      .then(res => {
        if (res.data.code === 1) {
          let list = res.data.data.list || []
          for (let i = 0; i < list.length; i ++) {
            list[i].createDate = formatDate(list[i].createDate)
          }
          this.total = res.data.data.totalRecord
          this.templates = list
        } else {
          this.$message.error(res.data.msg || '网络异常!')
        }
        this.loading = false
      })
      .catch(e => {
        this.loading = false
        this.$message.error('网络异常!')
      })
    }
  },
  created () {
    this.fetchTemplateList(1)
  }
}
</script>
<style lang="scss">
  .search-ai {
    table {
      .cell {
        text-align: center;
      }
    }
    .search {
      display: flex;
    }
  }
</style>
